package cn.com.wxd.web.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import cn.com.wxd.util.action.AccessLogUtil;

/**
 * Title:用户访问记录处理
 * Description:cn.com.wxd.listener
 * <p>
 * Company: Amumu管理平台
 * Copyright: Copyright (c) 2015
 * All right reserved.
 * Created time: 2016-4-14 上午11:48:23
 *
 * @author WangXuDong
 * @version 1.0
 */
public class OnlineCountListener implements HttpSessionListener {

    /**
     * 增加了在线用户并且加入访问记录
     */
    @Override
    public void sessionCreated(HttpSessionEvent event) {
        ServletContext context = event.getSession().getServletContext();
        Integer count = (Integer) context.getAttribute("ONLINECOUNT");//获取在线人数
        if (count == null) {
            count = 1;
        } else {
            count = count + 1;
        }
        context.setAttribute("ONLINECOUNT", count);//保存人数
        AccessLogUtil.setOnlineUser(count);
    }

    @Override
    /**
     * 减少了在线用户
     */
    public void sessionDestroyed(HttpSessionEvent event) {
        ServletContext context = event.getSession().getServletContext();
        Integer count = (Integer) context.getAttribute("ONLINECOUNT");
        if (count == null) {
            count = new Integer(1);
        } else {

            if (count <= 0) {
                count = 0;
            } else {
                count = count - 1;
            }
        }
        context.setAttribute("ONLINECOUNT", count);//保存人数
        AccessLogUtil.setOnlineUser(count);
    }

}
